#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include "usbcid.h"

int main( int argc, char *argv[] )
{
	int dev;
	int retval = 0;
	cid_ctrl_info ctrl_info;
	char buff[32];
	struct timespec req;

	if( argc < 2 ) {
		printf("Usage: %s FILENAME\n", argv[0]);
		return 0;
	}

	req.tv_sec = 0;
	req.tv_nsec = 200000000;	//0.2sec

	dev = open( argv[1], O_RDWR|O_NDELAY);
	if(dev >= 0) {
		printf("Test program started.\n");
		printf("Reading CID from %s ...\n", argv[1]);
		memset( &ctrl_info, 0, sizeof(cid_ctrl_info) );
		ctrl_info.pipe = 1;
		
		while(1) {
			retval = ioctl(dev, IOCTL_EZUSB_READ_CID, &ctrl_info);
			
			if(retval == 0) {
				if( strlen(ctrl_info.buff) ) {
					system("date");
					printf("Port %d: %s\n\n", ctrl_info.pipe, ctrl_info.buff);
				}
			}

			if( ctrl_info.pipe == 4 ) ctrl_info.pipe = 1;
			else ctrl_info.pipe++;
			
			nanosleep(&req, NULL);
		}
	}
	else printf("Error: can't open device file!\n");
}
